package jp.heartdokyun.activity;

import java.util.ArrayList;

import jp.heartdokyun.R;
import jp.heartdokyun.adapter.ImageAdapter;
import jp.heartdokyun.listener.GoBackListener;
import jp.heartdokyun.listener.TopBackListener;
import jp.heartdokyun.model.Level;
import jp.heartdokyun.util.Utils;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;

public class CardCollectionActivity extends Activity {
	private ArrayList<Integer> mListImageIDs;
	private ArrayList<Integer> mListThumbIDs;
	private ArrayList<String> mListTitles;
	private MediaPlayer mMediaPlayer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
		setContentView(R.layout.card_collection);
		fetchListCardImage();
		GridView gridview = (GridView) findViewById(R.id.gv_card_collection);
		gridview.setAdapter(new ImageAdapter(this, mListThumbIDs));
		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Intent itViewCard = new Intent(CardCollectionActivity.this,
						ViewCardActivity.class);
				itViewCard.putExtra("IMAGE_POSITION", position);
				itViewCard.putIntegerArrayListExtra("LIST_IMAGE_IDS",
						mListImageIDs);
				itViewCard.putStringArrayListExtra("LIST_CARD_TITLES",
						mListTitles);
				startActivity(itViewCard);
			}
		});

		ImageButton ibBack = (ImageButton) findViewById(R.id.ib_card_collection_back);
		ibBack.setOnClickListener(new GoBackListener(this));

		ImageButton ibo = (ImageButton) findViewById(R.id.ic_o);
		ibo.setOnClickListener(new TopBackListener(this));
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	void fetchListCardImage() {
		mListImageIDs = new ArrayList<Integer>();
		mListTitles = new ArrayList<String>();
		mListThumbIDs = new ArrayList<Integer>();
		// Level lv2 = Utils.getDataObj();
		for (Level lv : Utils.getDataObj()) {
			if (!lv.getTime().equals("")) {
				mListImageIDs.add(getResources().getIdentifier(
						lv.getType() + "_lv" + lv.getLevel(), "drawable",
						"jp.heartdokyun"));
				mListThumbIDs.add(getResources().getIdentifier(
						lv.getType() + "_lvs" + lv.getLevel(), "drawable",
						"jp.heartdokyun"));
				mListTitles.add(getResources().getString(R.string.game_tilte)
						+ " "
						+ getString(getResources().getIdentifier(
								"title_" + lv.getType(), "string",
								"jp.heartdokyun")) + " Lv." + lv.getLevel());
			} else {
				mListThumbIDs.add(getResources().getIdentifier("noimages",
						"drawable", "jp.heartdokyun"));
				mListImageIDs.add(getResources().getIdentifier("noimagel",
						"drawable", "jp.heartdokyun"));
				mListTitles.add(getResources().getString(R.string.game_tilte)
						+ " "
						+ getString(getResources().getIdentifier(
								"title_" + lv.getType(), "string",
								"jp.heartdokyun")) + " Lv." + lv.getLevel());
			}
		}

	}

	@Override
	public void onDestroy() {
		if (mMediaPlayer != null)
			mMediaPlayer.release();
		super.onDestroy();
	}

	@Override
	public void onResume() {
		// if(isViewCard){
		// mMediaPlayer = MediaPlayer.create(getApplicationContext(),
		// R.raw.other_bgm);
		// mMediaPlayer.setLooping(true);
		// }
		// if (MainActivity.isEnableBGM) {
		// mMediaPlayer.start();
		// } else {
		// if (mMediaPlayer.isPlaying()) {
		// mMediaPlayer.pause();
		// mMediaPlayer.seekTo(0);
		// }
		// }
		if (MainActivity.isEnableBGM) {
			mMediaPlayer = MediaPlayer.create(getApplicationContext(),
					R.raw.other_bgm);
			float volume = Utils.getBGMVolume(getApplicationContext());
			mMediaPlayer.setVolume(volume, volume);
			mMediaPlayer.setLooping(true);
			mMediaPlayer.start();
		}
		super.onResume();
	}

	@Override
	public void onPause() {
		// if (mMediaPlayer.isPlaying()) {
		// mMediaPlayer.pause();
		// mMediaPlayer.seekTo(0);
		// }
		// if(isViewCard){
		// mMediaPlayer.release();// }
		if (mMediaPlayer != null)
			mMediaPlayer.release();
		super.onPause();
	}
}